<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-select
          v-model="queryParams.deviceName"
          placeholder="请选择设备名称"
          clearable
        >
          <el-option
            v-for="dict in dict.type.device_name"
            :key="dict.id"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备型号" prop="deviceModel">
        <el-input
          v-model="queryParams.deviceModel"
          placeholder="请输入设备型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产编号" prop="deviceAssetNo">
        <el-input
          v-model="queryParams.deviceAssetNo"
          placeholder="请输入资产编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属车间" prop="deviceWorkshop">
        <el-select
          v-model="queryParams.deviceWorkshop"
          placeholder="请选择所属车间"
          clearable
        >
          <el-option
            v-for="dict in dict.type.product_order_workshop"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:manage:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:manage:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:manage:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:manage:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="manageList"
      border
      @selection-change="handleSelectionChange"
      :header-cell-style="{ background: '#6eaef5', color: '#ffffff' }"
      style="width: 100%"
    >
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column width="60" label="序号" align="center" prop="deviceId" />
      <el-table-column label="设备名称" align="center" prop="deviceName">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.device_name"
            :value="scope.row.deviceName"
          />
        </template>
      </el-table-column>
      <el-table-column label="设备型号" align="center" prop="deviceModel" />
      <el-table-column label="供应商" align="center" prop="deviceSupplier" />
      <el-table-column label="生产日期" align="center" prop="deviceDate" />
      <el-table-column label="负责人" align="center" prop="deviceManager" />
      <el-table-column label="维保周期(天)" align="center" prop="deviceRound" />
      <el-table-column
        label="操作"
        align="center"
        width="110"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button type="primary" @click="detail(scope.row.id)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备资产信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-select v-model="form.deviceName" placeholder="请选择设备名称">
            <el-option
              v-for="dict in dict.type.device_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备型号" prop="deviceModel">
          <el-input v-model="form.deviceModel" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="资产编号" prop="deviceAssetNo">
          <el-input v-model="form.deviceAssetNo" placeholder="请输入资产编号" />
        </el-form-item>
        <el-form-item label="生产日期" prop="deviceDate">
          <el-date-picker
            clearable
            v-model="form.deviceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生产日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="供应商" prop="deviceSupplier">
          <el-input v-model="form.deviceSupplier" placeholder="请输入供应商" />
        </el-form-item>
        <el-form-item label="所属车间" prop="deviceWorkshop">
          <el-select v-model="form.deviceWorkshop" placeholder="请选择所属车间">
            <el-option
              v-for="dict in dict.type.product_order_workshop"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="使用人" prop="deviceUser">
          <el-input v-model="form.deviceUser" placeholder="请输入使用人" />
        </el-form-item>
        <el-form-item label="负责人" prop="deviceManager">
          <el-input v-model="form.deviceManager" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="点检位置" prop="deviceCheck">
          <el-input v-model="form.deviceCheck" placeholder="请输入点检位置" />
        </el-form-item>
        <el-form-item label="维保周期" prop="deviceRound">
          <el-input v-model="form.deviceRound" placeholder="请输入维保周期" />
        </el-form-item>
        <el-form-item label="上次维保时间" prop="deviceLasttime">
          <el-date-picker
            clearable
            v-model="form.deviceLasttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上次维保时间"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listManage,
  getManage,
  delManage,
  addManage,
  updateManage,
  detailMes,
  mainTenanceMes,
  faultMes,
} from "@/api/system/manage";
import xq from "@/views/system/xq/index";

export default {
  name: "Manage",
  dicts: ["device_name", "product_order_workshop"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备资产信息管理表格数据
      manageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上次维保时间时间范围
      daterangeDeviceDate: [],
      // 上次维保时间时间范围
      daterangeDeviceLasttime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 9,
        deviceId: null,
        deviceName: null,
        deviceModel: null,
        deviceAssetNo: null,
        deviceDate: null,
        deviceSupplier: null,
        deviceWorkshop: null,
        deviceUser: null,
        deviceManager: null,
        deviceCheck: null,
        deviceRound: null,
        deviceDeadline: null,
        deviceLasttime: null,
      },
      dataList: [],
      // 设备故障信息
      fault: [],
      id: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备资产信息管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDeviceDate && "" != this.daterangeDeviceDate) {
        this.queryParams.params["beginDeviceDate"] =
          this.daterangeDeviceDate[0];
        this.queryParams.params["endDeviceDate"] = this.daterangeDeviceDate[1];
      }
      if (
        null != this.daterangeDeviceLasttime &&
        "" != this.daterangeDeviceLasttime
      ) {
        this.queryParams.params["beginDeviceLasttime"] =
          this.daterangeDeviceLasttime[0];
        this.queryParams.params["endDeviceLasttime"] =
          this.daterangeDeviceLasttime[1];
      }
      listManage(this.queryParams).then((response) => {
        console.log(response);
        this.manageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        deviceId: null,
        deviceName: null,
        deviceModel: null,
        deviceAssetNo: null,
        deviceDate: null,
        deviceSupplier: null,
        deviceWorkshop: null,
        deviceUser: null,
        deviceManager: null,
        deviceCheck: null,
        deviceRound: null,
        deviceDeadline: null,
        deviceLasttime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDeviceDate = [];
      this.daterangeDeviceLasttime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备资产信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getManage(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备资产信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateManage(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManage(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除设备资产信息管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delManage(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/manage/export",
        {
          ...this.queryParams,
        },
        `manage_${new Date().getTime()}.xlsx`
      );
    },
    // 获取id，发送请求并跳转页面
    detail(id) {
      this.$router.push({
        path: "/xq",
        component: xq,
        redirect: "noRedirect",
        hidden: false,
        alwaysShow: true,
        query: { listId: id, manageList: this.manageList },
      });
    },
  },
};
</script>
<style scoped></style>
